package com.googlecode.biglog.appender.jdbc;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.helpers.FormattingInfo;
import org.apache.log4j.helpers.PatternConverter;
import org.apache.log4j.spi.LoggingEvent;

public class PatternParser extends org.apache.log4j.helpers.PatternParser {

	public PatternParser(String pattern) {
		super(pattern);
	}

	@Override
	protected void finalizeConverter(char c) {
		if (c != 'm') {
			super.finalizeConverter(c);
			return;
		}
		PatternConverter pc = new MessagePatternConverter(formattingInfo);
		currentLiteral.setLength(0);
		addConverter(pc);
	}

	private static class MessagePatternConverter extends PatternConverter {
		MessagePatternConverter(FormattingInfo formattingInfo) {
			super(formattingInfo);
		}
		public String convert(LoggingEvent event) {
			return StringEscapeUtils.escapeSql(event.getRenderedMessage());
		}
	}
}
